import Foundation

public struct YMLog {
    public private(set) var text = "Hello, World!"

    public init() {
        Print("")
    }
    
    /// 获取log文件地址列表
    /// - Returns: [URL] log文件地址列表
    public func logFileList() -> [URL]? {
        let fileManager = FileManager.default
        if let logDirectory = getDirectory() {
            do {
                let fileURLs = try fileManager.contentsOfDirectory(at: logDirectory, includingPropertiesForKeys: [.contentModificationDateKey], options: [.skipsHiddenFiles])
                let sortedFileURLs = try fileURLs.sorted { (file1, file2) -> Bool in
    #if os(OSX)
                    if #available(macOS 13.0, *) {
                        let file1Arr = try fileManager.attributesOfItem(atPath: file1.path())
                        let file1ModDate = file1Arr[.modificationDate] as! Date
                        
                        let file2Arr = try fileManager.attributesOfItem(atPath: file2.path())
                        let file2ModDate = file2Arr[.modificationDate] as! Date
                        
                        return file1ModDate > file2ModDate
                    } else {
                        let file1Arr = try fileManager.attributesOfItem(atPath: file1.path)
                        let file1ModDate = file1Arr[.modificationDate] as! Date
                        
                        let file2Arr = try fileManager.attributesOfItem(atPath: file2.path)
                        let file2ModDate = file2Arr[.modificationDate] as! Date
                        
                        return file1ModDate > file2ModDate
                    }
    #elseif os(iOS)
                    let file1Arr = try fileManager.attributesOfItem(atPath: file1.path)
                    let file1ModDate = file1Arr[.modificationDate] as! Date
                    
                    let file2Arr = try fileManager.attributesOfItem(atPath: file2.path)
                    let file2ModDate = file1Arr[.modificationDate] as! Date
                    
                    return file1ModDate > file2ModDate
    #endif
                }
                
                return sortedFileURLs
            } catch {
                print("\(error)")
                return nil
            }
        }
        return nil
    }
}
